package com.example.yang.yixin.Activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.blankj.utilcode.util.Utils;
import com.example.yang.yixin.Adapter.MusicGridAdapter;
import com.example.yang.yixin.R;
import com.example.yang.yixin.Utils.Class_info;
import com.example.yang.yixin.Utils.UserUtils;
import com.example.yang.yixin.Views.InputView;

import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import static android.support.constraint.Constraints.TAG;

public class MeActivity extends BaseActivity{
private TextView tv_username;
  LinearLayout re;

List list=new ArrayList();
private ImageView mIv_back;
private SharedPreferences pref;
View view;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_me);
        tv_username=findViewById(R.id.tv_username);
        mIv_back=findViewById(R.id.iv_back);
        mIv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MeActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });
        initView();
       pref=getSharedPreferences("user",MODE_PRIVATE);
    }

    private void initView() {
        initNarvBar(true,"个人中心",false);
        String username=getIntent().getStringExtra("data");
        tv_username.setText(username);
    }

    public void onChangeClick(View view) {
        UserUtils.ChangePassword(this);
    }

    public void onLogoutClick(View view) {
        UserUtils.Logout(this);
    }

    public void onMeClick(View view) {
        Intent intent=new Intent();
        String username=getIntent().getStringExtra("data");
        intent.putExtra("data",username);
        intent.setClass(MeActivity.this,InformationActivity.class);
        startActivity(intent);
    }

    public void onBankeClick(View view) {
        Intent intent=new Intent();
        String username=getIntent().getStringExtra("data");
        intent.putExtra("data",username);
        intent.setClass(MeActivity.this,CreateBanKeActivity.class);
        startActivity(intent);
    }

    public void onAdd_BankeClick(View view) {
        if(pref.getBoolean("saved",true)){
            Snackbar.make(view,"请先完善您的个人信息哦！",Snackbar.LENGTH_LONG).setAction("我知道了", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent();
                    String username=getIntent().getStringExtra("data");
                    intent.putExtra("data",username);
                    intent.setClass(MeActivity.this,InformationActivity.class);
                    startActivity(intent);
                }
            }).show();

        }else {
            re = (LinearLayout) getLayoutInflater().inflate(R.layout.login, null);
            AlertDialog.Builder dialog = new AlertDialog.Builder(MeActivity.this);
            dialog.setTitle("加入班课");
            dialog.setView(re);
            dialog.setNeutralButton("确定", new LoginClick());
            dialog.setPositiveButton("取消", new CencleClick());
            dialog.create();
            dialog.show();


        }


    }



    public void onAboutusClick(View view) {
    Intent intent=new Intent();
    intent.setClass(MeActivity.this,FacedemoActivity.class);
    startActivity(intent);

    }
    public void onFaceSet(View view) {
         Intent intent=new Intent();
         intent.setClass(MeActivity.this,FaceActivity.class);
         startActivity(intent);
    }

    public void onQuestionClick(View view) {

    }
    Handler handler = new Handler()

    {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    Snackbar.make(getWindow().getDecorView(),"您已在该班课哦！",Snackbar.LENGTH_LONG).setAction("我知道了", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                        }
                    }).show();

                    break;
                case 2:
                    Snackbar.make(getWindow().getDecorView(),"服务器有点忙哦，稍后再试！",Snackbar.LENGTH_LONG).setAction("我知道了", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                        }
                    }).show();
                    break;
                case 3:
                    Snackbar.make(getWindow().getDecorView(),"没有找到与该号码匹配的班课号！",Snackbar.LENGTH_LONG).setAction("我知道了", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                        }
                    }).show();
                    break;
                case 4:
                    final ProgressDialog progressDialog = new ProgressDialog(MeActivity.this);
                    progressDialog.setTitle("小依来报");
                    progressDialog.setMessage("努力申请中...");
                    progressDialog.setCancelable(false);
                    progressDialog.show();
                    new Thread() {
                        @Override
                        public void run() {
                            try {
                                super.run();
                                Thread.sleep(1200);
                                progressDialog.dismiss();

                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }.start();

                    Snackbar.make(getWindow().getDecorView(),"成功加入班课，快去首页刷新一下吧！",Snackbar.LENGTH_LONG).setAction("我知道了", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                        }
                    }).show();


                    break;
            }

        }
    };


    private class LoginClick implements DialogInterface.OnClickListener {

        public void onClick(DialogInterface dialog, int which) {
           InputView editText1=(InputView)re.findViewById(R.id.input_ttt) ;
           getServerData(editText1.getInputStr());
            final Message msg = handler.obtainMessage();
           new Thread(new Runnable() {
               @Override
               public void run() {
                   try {
                       Thread.sleep(500);
                       if(list.get(0).equals("true")){
                                msg.what=4;
                                handler.sendMessage(msg);
                       }
                       else if(list.get(0).equals("false")){
                           msg.what=2;
                           handler.sendMessage(msg);
                       }
                       else if(list.get(0).equals("joined")){
                           msg.what=1;
                           handler.sendMessage(msg);
                       }
                       else if(list.get(0).equals("notexist")){
                           msg.what=3;
                           handler.sendMessage(msg);
                       }
                   } catch (InterruptedException e) {
                       Snackbar.make(getWindow().getDecorView(),"发生异常！",Snackbar.LENGTH_LONG).setAction("我知道了", new View.OnClickListener() {
                           @Override
                           public void onClick(View v) {

                           }
                       }).show();
                   }

               }
           }).start();




        }
    }





private class CencleClick implements DialogInterface.OnClickListener {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    }


    private void getServerData(String s) {
        String jsonurl = "http://47.106.141.161/MyFirstAndroidApp/TakeinServlet?username="+getIntent().getStringExtra("data")+"&bkpassword="+s;
        RequestQueue mQueue = Volley.newRequestQueue(this);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(jsonurl, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {


                    String flag=response.getString("flag") ;
                    list.add(0,flag);




                } catch (JSONException e) {

                    Log.e("json错误",e.getMessage());
                }
            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.e("TAG错误", volleyError.getMessage(), volleyError);
                    }
                }) {
            protected Response<JSONObject> paraeNetworkResponse(NetworkResponse response) {
                try {
                    JSONObject jsonObject = new JSONObject(new String(response.data, "utf-8"));
                    return Response.success(jsonObject, HttpHeaderParser.parseCacheHeaders(response));
                }catch (JSONException je) {
                    return Response.error(new ParseError(je));
                } catch (UnsupportedEncodingException e) {
                    return Response.error(new ParseError(e));
                }

            }
        };


        mQueue.add(jsonObjectRequest);

    }

    public void onBackPressed() {

        super.onBackPressed();
    }
}
